﻿using Sirenix.OdinInspector;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 触发类型
/// </summary>
[LabelText("触发类型")]
public enum TriggerType
{
    [LabelText("自动触发")]
    Auto,
    [LabelText("点击触发")]
    Click,
    [LabelText("拖动触发")]
    Drag,
    [LabelText("长按触发")]
    LongPress,
   
}

public class UIType : MonoBehaviour
{
	
	/// <summary>
	/// 变量名字
	/// </summary>
	private string VariableName
	{
		get { return gameObject.name.LowercaseFirst(); }
        set {  }
	}
    [Tooltip ("是否构建")] 
    public bool orbuild = false;

    [Tooltip("是否矩形遮罩")]
    public bool orRect = true;

    
    public TriggerType trigger;
    /// <summary>
    /// 当前脚本所在UI是否已经生成在脚本中
    /// </summary>
    public bool OrBuild
    {
        get { return orbuild; }
    }


    /// <summary>
    /// 物体父物体名词集合
    /// </summary>
    [HideInInspector]
    public List<string> parentNameList = new List<string>();

	/// <summary>
	/// UI类型
	/// </summary>
	private string ComponentName
	{
		get
		{
			if (null != GetComponent<ScrollRect>())
				return "ScrollRect";
			if (null != GetComponent<InputField>())
				return "InputField";
			if (null != GetComponent<Button>())
				return "Button";
            if (null != GetComponent<UIButtonExtension>())
                return "Button";
            if (null != GetComponent<Text>())
				return "Text";
            if (null != GetComponent<UITextExtension>())
                return "Text";
            if (null != GetComponent<RawImage>())
				return "RawImage";
			if (null != GetComponent<Toggle>())
				return "Toggle";
			if (null != GetComponent<Slider>())
				return "Slider";
			if (null != GetComponent<Scrollbar>())
				return "Scrollbar";
			if (null != GetComponent<Image>())
				return "Image";
            if (null != GetComponent<UIImageExtension>())
                return "Image";
            if (null != GetComponent<ToggleGroup>())
				return "ToggleGroup";
			if (null != GetComponent<Animator>())
				return "Animator";
			if (null != GetComponent<Canvas>())
				return "Canvas";
			if (null != GetComponent<RectTransform>())
				return "RectTransform";

			return "Transform";
		}
	}
	
	/// <summary>
	/// 变量声明内容
	/// </summary>
	public string StatementContent
	{
		get
        {

            var GameObjectPath = "";
            var VariableNamePrefix = "";
            for (int i = parentNameList.Count; i > 0; i--)
            {
                GameObjectPath = parentNameList[i - 1] + "/"+ GameObjectPath;

                VariableNamePrefix += parentNameList[i - 1].GetcaseFirst();
            }
           

            return "private"+" "+ ComponentName + " " + VariableNamePrefix + "_" + VariableName+";";
        }
	}

	/// <summary>
	/// 变量绑定内容
	/// </summary>
	public string BindVariableContent
	{
		get
		{
            var GameObjectPath = "";
            var VariableNamePrefix = "";
            for (int i = parentNameList.Count; i > 0; i--)
            {
                GameObjectPath = parentNameList[i - 1] + "/"+ GameObjectPath;

                VariableNamePrefix += parentNameList[i - 1].GetcaseFirst();
            }
            //Debug.Log(gameObject.name+"数量为-----"+ parentNameList.Count+"----"+ GameObjectPath);
            GameObjectPath += gameObject.name;
            //Debug.Log("最后是"+GameObjectPath);
            var content = (VariableNamePrefix + "_" + VariableName) + " = " + "transform.Find(\"" + GameObjectPath + "\")";
			var bindProperty = "GetComponent<" + ComponentName + ">();";
			return content+"."+bindProperty;
		}
	}

    

    /// <summary>
    /// 变量绑定方法
    /// </summary>
    public string BindMethodContent
    {
        get
        {
            var content = "";
            var GameObjectPath = "";
            var VariableNamePrefix = "";
            for (int i = parentNameList.Count; i > 0; i--)
            {
                GameObjectPath = parentNameList[i - 1] + "/"+ GameObjectPath;

                VariableNamePrefix += parentNameList[i - 1].GetcaseFirst();
            }
            switch (ComponentName)
            {
                case "Button":
                    //content = (VariableNamePrefix + "_" + VariableName) + "." + "onClick.AddListener(On"+ (VariableNamePrefix.UppercaseFirst() + "_" + VariableName.UppercaseFirst()) + "Clicked);";
                    //content = "EventTriggerListener.GetListener("+(VariableNamePrefix + "_" + VariableName) + ".gameObject)." + "onClick+= (go) =>\n\t\t{\n\t\t\tOn" + (VariableNamePrefix.UppercaseFirst() + "_" + VariableName.UppercaseFirst()) + "Clicked();\n\t\t};";
                    content = (VariableNamePrefix + "_" + VariableName) + "." + "AddClickMethod(On" + (VariableNamePrefix.UppercaseFirst() + "_" + VariableName.UppercaseFirst()) + "Clicked);";
                    break;
            }
            
            
            return content;
        }
    }

    /// <summary>
    /// 方法
    /// </summary>
    public string MethodContent
    {
        get
        {
            var content = "";
            var GameObjectPath = "";
            var VariableNamePrefix = "";
            for (int i = parentNameList.Count; i > 0; i--)
            {
                GameObjectPath = parentNameList[i - 1] + "/"+ GameObjectPath;

                VariableNamePrefix += parentNameList[i - 1].GetcaseFirst();
            }
            switch (ComponentName)
            {
                case "Button":
                    //content = "//"+ (VariableNamePrefix + "_" + VariableName) + "按下时调用此方法\n\t" + "private void On"+ (VariableNamePrefix.UppercaseFirst() + "_" + VariableName.UppercaseFirst()) + "Clicked()\n\t{\n\t     //TODO:添加自己的代码\n\t"+ "    Debug.Log(\"点击了" + (VariableNamePrefix + "_" + VariableName) + "按钮\");" + " \n\t}\n";
                    content = "//" + (VariableNamePrefix + "_" + VariableName) + "按下时调用此方法\n\t" + "private void On" + (VariableNamePrefix.UppercaseFirst() + "_" + VariableName.UppercaseFirst()) + "Clicked(GameObject game)\n\t{\n\t     //TODO:添加自己的代码\n\t" + "    Debug.Log(\"点击了\"+ "+"game.name" +"+\"按钮\");" + " \n\t}\n";

                    break;
            }
            

            return content;
        }
    }

    /// <summary>
    /// 设置UI已经添加
    /// </summary>
    public void SetBuild(bool orBuild=true)
    {
        orbuild = orBuild;
    }

    /// <summary>
    /// 向上遍历获取物体路径信息
    /// </summary>
    /// <param name="gameObject"></param>
    public void GetGameobjectPath(GameObject gameObject)
    {
        
        if (gameObject.transform.parent != null)
        {

            if (!gameObject.transform.parent.gameObject.name.Contains("Form"))
            {
               
                GetGameobjectPath(gameObject.transform.parent.gameObject);
                parentNameList.Add(gameObject.transform.parent.gameObject.name);
            }

        }


    }
}
